package com.molinari.coding.activities.photo;

import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import com.molinari.androidstructure.activity.BaseActivity;
import com.molinari.androidstructure.file.UtilFile;
import com.molinari.androidstructure.hardware.camera.PreviewCamera;
import com.molinari.coding.R;

public class PhotoActivity extends BaseActivity{

	Button btnChange;
	ImageView photo;
	
	private static final String CODE_PHOTO = "code_photo.jpeg";
	private PreviewCamera mCameraView;
	
	@Override
	protected int getIdLayout() {
		boolean hasExternalStoragePrivateFile = UtilFile.hasExternalStoragePrivateFile(getApplicationContext(), Environment.DIRECTORY_PICTURES, CODE_PHOTO);
		if(hasExternalStoragePrivateFile){
			return R.layout.activity_photo;
		}else{
			return 0;
		}
	}

	@Override
	protected void onCreateCustom(Bundle savedInstanceState) throws Exception {
		
		boolean hasExternalStoragePrivateFile = UtilFile.hasExternalStoragePrivateFile(getApplicationContext(), Environment.DIRECTORY_PICTURES, CODE_PHOTO);
		if(hasExternalStoragePrivateFile){
			btnChange = loadButton(btnChange, R.id.btnPhotoChange);
			
			if(btnChange != null){
				btnChange.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						 showCamera();
					}
				});
			}
			
			photo = (ImageView) load(photo, R.id.imgPhotoCode);
			if(photo != null){
				String pathFoto = getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/" + CODE_PHOTO;
				Uri imgUri=Uri.parse("file:///" + pathFoto);
				photo.setImageURI(imgUri);
			}
		}else{
			  showCamera();
		}
	}

	private void showCamera() {
		// Force screen in landscape mode as showing a video in 
		// potrait mode is not easily doable on all devices
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		        
		// Hide window title and go fullscreen
//		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		mCameraView= new PreviewCamera(this);
		mCameraView.setName(CODE_PHOTO);
		setContentView(mCameraView);
	}

	@Override
	public int getIdMenu() {
		return 0;
	}

}
